package com.share.common.tools.password;


import com.share.common.tools.ID.IDutils;
import org.apache.log4j.Logger;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

/**
 * 生成随即密码的工具类
 * Created by er on 17-8-9.
 */
public class PassWordUtil {
    private static final Logger logger = Logger.getLogger(PassWordUtil.class);


    /**
     * 对密码加密程序
     *
     * @param password password
     * @return 加密后的结果
     */
    public static String toMD5(final String password) {
        String reStr = null;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(password.getBytes());
            StringBuffer stringBuffer = new StringBuffer();
            for (byte b : bytes) {
                int bt = b & 0xff;
                if (bt < 16) {
                    stringBuffer.append(0);
                }
                stringBuffer.append(Integer.toHexString(bt));
            }
            reStr = stringBuffer.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return reStr;
    }

    /**
     * 得到随即秘密
     *
     * @return 根据时间戳得到随即密码
     */
    public static String getRandomPassword() {
        return PassWordUtil.toMD5(String.valueOf(IDutils.genItemId()));
    }

    /**
     * 根据uuid来生存随即密码
     *
     * @return 密码
     */
    public static String getUUIDPassword() {
        return PassWordUtil.toMD5(IDutils.getUUID());
    }


    public static void main(String[] args) {
 /*       String password = "12321";
        String md5 = PassWordUtil.toMD5(password);
        logger.info(md5);*/

        //logger.info(PassWordUtil.getUUIDPassword());

        logger.info(PassWordUtil.getRandomWord(7));


    }


    /**
     * @param nums 几位数字的汉字
     * @return 汉字
     */
    public static String getRandomWord(final int nums) {
        Random ran = new Random();
        int delta = 0x9fa5 - 0x4e00 + 1;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < nums; i++) {
            sb.append((char) (0x4e00 + ran.nextInt(delta)));
        }
        return sb.toString();
    }

    /**
     * sha 算法
     * @param strText strText
     * @param strType 加密方式
     * @return String
     */
    private static String SHA(final String strText, final String strType) {
        // 返回值
        String strResult = null;

        // 是否是有效字符串
        if (strText != null && strText.length() > 0) {
            try {
                // SHA 加密开始
                // 创建加密对象 并傳入加密類型
                MessageDigest messageDigest = MessageDigest.getInstance(strType);
                // 传入要加密的字符串
                messageDigest.update(strText.getBytes());
                // 得到 byte 類型结果
                byte byteBuffer[] = messageDigest.digest();

                // 將 byte 轉換爲 string
                StringBuffer strHexString = new StringBuffer();
                // 遍歷 byte buffer
                for (int i = 0; i < byteBuffer.length; i++) {
                    String hex = Integer.toHexString(0xff & byteBuffer[i]);
                    if (hex.length() == 1) {
                        strHexString.append('0');
                    }
                    strHexString.append(hex);
                }
                // 得到返回結果
                strResult = strHexString.toString();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }

        return strResult;
    }


    /**
     * 传入文本内容，返回 SHA-256 串
     *
     * @param strText
     * @return
     */
    public static String SHA256(final String strText) {
        return PassWordUtil.SHA(strText, "SHA-256");
    }


    /**
     * 传入文本内容，返回 SHA-512 串
     *
     * @param strText
     * @return
     */
    public String SHA512(final String strText) {
        return PassWordUtil.SHA(strText, "SHA-512");
    }








}
